package cn.gaozhong.ymcc.mq.consumer;

import cn.gaozhong.ymcc.service.ICourseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup="course-order-paytimeout-consumer",
        topic="paytimeout-topic",
        selectorExpression="paytimeout-tag",
        messageModel = MessageModel.BROADCASTING  //  现在我们需要使用广播模式消费
)
@Slf4j
public class PayTimeOutConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService courseOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("订单微服务：接收到支付超时消息");
        byte[] body = messageExt.getBody();
        if(body == null || body.length == 0){
           return;//消息不对，直接签收
        }
        String orderNo = new String(body, StandardCharsets.UTF_8);
        courseOrderService.cancelOrder(orderNo);
    }
}